import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { HttpService } from 'src/app/service/http.service';
import { NavController } from '@ionic/angular';
import { EventService } from 'src/app/service/event.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { Storage } from '@ionic/storage';
import * as $ from 'jquery';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { ToolService } from 'src/app/service/tool.service';
@Component({
  selector: 'app-findpsw',
  templateUrl: './findpsw.page.html',
  styleUrls: ['./findpsw.page.scss'],
})
export class FindpswPage implements OnInit {
  public onLoginForm: FormGroup;
  public gap = 0;
  private timer01;
  constructor(private formBuilder: FormBuilder, private http: HttpService, private navCtrl: NavController, private eventS: EventService, private popWin: PopwinService, private store: Storage) { }

  ngOnInit() {
    this.onLoginForm = this.formBuilder.group({
      'username': [null, Validators.compose([
        Validators.required
      ])],
      'imgcode': [null, Validators.compose([
        Validators.required
      ])],
      'password': [null, Validators.compose([
        Validators.required
      ])]
    });
  }
  /**
   * 确定找回密码
   */
  onSubmit() {
    let that = this;
    let obj = "{\"username\":\"" + this.onLoginForm.value.username + "\",\"type\":\"login\",\"code\":\"" + this.onLoginForm.value.imgcode + "\",\"password\":\"" + this.onLoginForm.value.password + "\"}";
    this.http.get(UrlService.LOGIN_MODIFYPWD, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        that.popWin.toast(data.message);
        that.onLogin();
      } else {
        that.popWin.toast(data.message);
      }
    }, error => {
      that.popWin.toast(ToolService.newError(error));
    });
  }
  /**
   * 获取邮箱验证码
   */
  getCode() {
    let that = this;
    if (that.gap != 0) {
      return;
    }
    if (ToolService.isEmpty(this.onLoginForm.value.username)) {
      this.popWin.toast("请输入用户名");
      return;
    }
    let obj = "{\"username\":\"" + this.onLoginForm.value.username + "\",\"type\":\"login\"}";
    this.http.get(UrlService.LOGIN_FINDPWD, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        that.popWin.toast(data.message);
        let tempRes = ToolService.aesDecodeToJson(data.data);
        that.gap = tempRes.gap;
        clearInterval(this.timer01);
        that.count(that.gap);
      } else {
        that.popWin.toast(data.message);
      }
    }, error => {
      that.popWin.toast(ToolService.newError(error));
    });
  }

  count(val) {
    let that = this;
    this.timer01 = setInterval(function () {
      val--;
      if (val <= 0) {
        clearInterval(that.timer01);
      }
      that.gap = val;
    }, 1000);
  }
  onLogin() {
    this.backClick();
  }
  /**
   * 注册
   */
  registPage() {
    if(ConfigService.isAgent==1){
      this.navCtrl.navigateForward("/registagent");
    }else{
      this.navCtrl.navigateForward("/regist");
    }
    
  }
  /**
   * 返回
   */
  backClick() {
    this.navCtrl.back();
  }

}
